<template>
	<view>
		<nav-header title="修改群名称" :show-right="false" v-if="type=='name'">
		</nav-header>
		
		<nav-header title="修改群备注" :show-right="false" v-if="type=='remark'">
		</nav-header>

		<view class="name">
			<view class="form">
				<uni-forms :modelValue="formData" :rules="rules">
					<uni-forms-item name="name" label="群名称:" v-if="type=='name'">
						<uni-easyinput type="text" @input="sumFontNum" maxlength="30" v-model="formData.name"
							placeholder="请输入内容" :inputBorder="false" />
					</uni-forms-item>
					<uni-forms-item name="name" label="群备注:" v-if="type=='remark'">
						<uni-easyinput type="text" @input="sumFontNum" maxlength="30" v-model="formData.name"
							placeholder="请输入内容" :inputBorder="false" />
					</uni-forms-item>
				</uni-forms>
				<view class="tip">
					<text>{{fontNum}}/30</text>
				</view>
				<view class="edit-count" v-if="type=='name'">
					<view>
						<text>本月还可修改群名：0/1</text>
						<text class="kr">增加次数</text>
					</view>
				</view>
			</view>
			<!-- <view class="commit">
				<button @click="submitForm" v-if="type=='name'">修改</button>
				<button @click="submitForm2" v-if="type=='remark'">修改</button>
			</view> -->
			<view class="commit">
				<view class="button-style">
					<button class="btncolor" @click="submitForm" v-if="type=='name'">
						<text class="text-md-Medium" style="color: #fff;">修改</text>
					</button>
					<button class="btncolor" @click="submitForm2" v-if="type=='remark'">
						<text class="text-md-Medium" style="color: #fff;">修改</text>
					</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData: {
					name: ""
				},
				fontNum: 0,
				groupId: 0,
				type:"",
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '请输入群名称',
						}]
					},
				},
			}
		},
		methods: {
			sumFontNum(e) {
				this.fontNum = e.length
			},
			submitForm() {
				if(this.formData.name == ""){
					uni.showToast({
						title: "群名称不能为空",
						icon: 'error'
					})
					return;
				}
				let data={
					id:this.groupId,
					name:this.formData.name
				}
				this.$http({
					url:"/group/modify",
					method:"PUT",
					data:data
				}).then((data)=>{
					console.log(data)
					uni.showToast({
						title: "修改成功",
						icon: 'none'
					})
					setTimeout(() => {
						// 回退并刷新
						let pages = getCurrentPages();
						let prevPage = pages[pages.length - 2];
						prevPage.$vm.loadGroupInfo();
						uni.navigateBack();
					}, 1000);
				})
			},
			submitForm2(){
				if(this.formData.name == ""){
					uni.showToast({
						title: "群名称不能为空",
						icon: 'error'
					})
					return;
				}
				let data={
					id:this.groupId,
					remarkGroupName:this.formData.name
				}
				this.$http({
					url:"/group/modify",
					method:"PUT",
					data:data
				}).then((data)=>{
					console.log(data)
					uni.showToast({
						title: "修改成功",
						icon: 'none'
					})
					setTimeout(() => {
						// 回退并刷新
						let pages = getCurrentPages();
						let prevPage = pages[pages.length - 2];
						prevPage.$vm.loadGroupInfo();
						uni.navigateBack();
					}, 1000);
				})
			}
		},
		onLoad(options) {
			this.groupId = options.id
			this.type=options.type
			this.formData.name=options.name
			this.fontNum=this.formData.name.length
		},
	}
</script>

<style lang="scss" scoped>
	.name {
		width: 100%;

		.form {
			padding: 30rpx;

			.tip {
				display: flex;
				align-items: center;
				justify-content: flex-end;
				font-size: 25rpx;
				color: #aaa;
			}

			.edit-count {
				color: #5f5f5f;
				font-size: 30rpx;

				.kr {
					color: #d90000;
					margin-left: 20rpx;
				}
			}
		}

		// .commit {
		// 	button {
		// 		background-color: #0389ff;
		// 		color: #fff;
		// 		width: 92%;
		// 		height: 75rpx;
		// 		line-height: 75rpx;
		// 		font-size: 30rpx;
		// 	}
		// }
		
		.commit {
			width: 100%;
			position: fixed;
			bottom: 42rpx;
			left: 0;
			display: flex;
			justify-content: center;
			align-items: center;
			.button-style{
				padding: 24rpx 32rpx;
				width: 100%;
			}
			.btncolor {
				width: 100%;
				background-color: $brand-500;
				color: $white;
				border-radius: 34rpx;
				padding: 8rpx 24rpx;
				line-height: 60rpx;
				text-align: center;
			}
		}
	}
</style>